<?
require_once('version.inc');
require_once('settings.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');

function calendar($date, $target) {
	//If no parameter is passed use the current date.
	if($date == null)
	   $date = getDate();

	$day = $date["mday"];
	$month = $date["mon"];
	$month_name = $date["month"];
	$year = $date["year"];

	$this_month = getDate(mktime(0, 0, 0, $month, 1, $year));
	$next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year));

	//Find out when this month starts and ends.
	$first_week_day = $this_month["wday"];
	$days_in_this_month = daysinmonth($this_month["mon"], $this_month["mday"]);
	$calendar_html = "<table class=\"cal\">\n";

	$calendar_html .= "<tr><td class=\"cal_center\" colspan=\"7\">" .
			$month_name . " " . $year . "</td></tr>\n";

	$calendar_html .= "<tr><td>So</td><td>Mo</td><td>Di</td>";
	$calendar_html .= "<td>Mi</td><td>Do</td><td>Fr</td>";
	$calendar_html .= "<td>Sa</td></tr>\n";
	$calendar_html .= "<tr>";

	//Fill the first week of the month with the appropriate number of blanks.
	for($week_day = 0; $week_day < $first_week_day; $week_day++) {
	   $calendar_html .= "<td class=\"cal\"> </td>\n";
	}

	$week_day = $first_week_day;

	for($day_counter = 1; $day_counter <= $days_in_this_month; $day_counter++) {
	   $week_day %= 7;

	   if($week_day == 0)
	      $calendar_html .= "</tr>\n<tr>";

	   //Do something different for the current day.
	   $dstr = $day_counter . "." . $month . "." . $year;

	   if($day == $day_counter) {
	      $calendar_html .= "<td align=\"center\"><a href=\"javascript:SetValue('";
	      $calendar_html .= $target . "','" . $dstr . "')\"><b>" . $day_counter . "</b></a></td>\n";
	   } else {
	      $calendar_html .= "<td class=\"cal_center\">&nbsp;";
	      $calendar_html .= "<a href=\"javascript:SetValue('" . $target . "','" . $dstr . "')\">";
	      $calendar_html .= $day_counter . "</td>\n";
	   }

	   $week_day++;
	}

	$calendar_html .= "</tr>\n";
	$calendar_html .= "</table>\n";

	return($calendar_html);
}

/*
 * Steuerung des Kalenders
 */
$target = $_REQUEST['target'];
$month = $_REQUEST['month'];
$year = $_REQUEST['year'];
$mback = $_REQUEST['mback'];
$mforw = $_REQUEST['mforw'];
$yback = $_REQUEST['yback'];
$yforw = $_REQUEST['yforw'];
$edit = $_REQUEST['edit'];

if (isset($edit)) {
   $datum = getDate($edit);
}

if (isset($month) && isset($year)) {
   $datum = getDate(mktime(0, 0, 0, $month, 1, $year));
} else if (!isset($datum)) {
   $datum = getDate();
}

if (isset($mback)) {
   $month = $datum["mon"];
   $year = $datum["year"];

   $month--;

   if ($month < 1) {
      $month = 12;
      $year--;
   }

   $datum = getDate(mktime(0, 0, 0, $month, 1, $year));
}

if (isset($yback)) {
   $month = $datum["mon"];
   $year = $datum["year"];
   $year--;
   $datum = getDate(mktime(0, 0, 0, $month, 1, $year));
}

if (isset($mforw)) {
   $month = $datum["mon"];
   $year = $datum["year"];

   $month++;

   if ($month > 12) {
      $month = 1;
      $year++;
   }

   $datum = getDate(mktime(0, 0, 0, $month, 1, $year));
}

if (isset($yforw)) {
   $month = $datum["mon"];
   $year = $datum["year"];
   $year++;
   $datum = getDate(mktime(0, 0, 0, $month, 1, $year));
}

$month = $datum["mon"];
$year = $datum["year"];

//echo "<form name=\"cal\" action=\"calendar.php\" method=\"post\">\n";
echo "<form name=\"calendar\">\n";
echo "<input type=\"hidden\" name=\"header\" value=\"1\">\n";
echo "<input type=\"hidden\" name=\"target\" value=\"$target\">\n";
echo "<input type=\"hidden\" name=\"month\" value=\"$month\">\n";
echo "<input type=\"hidden\" name=\"year\" value=\"$year\">\n";
echo "<table class=\"input\"><tr><td>";
echo "<table border=0 cellspacing=0 cellpadding=0><tr>";
echo "<td><input type=\"submit\" name=\"yback\" value=\"<<<\"></td>\n";
//echo "<td>";
//Button("<<<", "cal", "yback", "1");
//echo "</td>";
echo "<td><input type=\"submit\" name=\"mback\" value=\"<--\"></td>\n";
//echo "<td>";
//Button("<--", "cal", "mback", "1");
//echo "</td>";
echo "</tr></table>\n";
echo "</td><td valign=\"right\">";
echo "<table border=0 cellspacing=0 cellpadding=0><tr>";
echo "<td><input type=\"submit\" name=\"mforw\" value=\"-->\"></td>\n";
//echo "<td>";
//Button("-->", "cal", "mforw", "1");
//echo "</td>";
echo "<td><input type=\"submit\" name=\"yforw\" value=\">>>\"></td>\n";
//echo "<td>";
//Button(">>>", "cal", "yforw", "1");
//echo "</td>";
echo "</tr></table>\n";
echo "</td></tr></table>\n";
echo calendar($datum, $target);
echo "<table class=\"indent\"><tr><td>";
ButtonProg("Schliessen", "self.close()");
echo "</td></tr></table>";
//echo "<input type=\"button\" name=\"close\" value=\"Schliessen\" onClick=\"javascript:self.close()\">\n";
echo "</form>\n";

require('footer.inc');
?>
